package problems.daily.y2022m7;

import java.util.Arrays;

/**
 * @author habitplus
 * @since 2022/7/27 21:14
 **/
public class D20220728_1331 {
    public int[] arrayRankTransform(int[] arr) {
        if (arr == null || arr.length < 1) {
            return arr;
        }

        int n = arr.length;
        int[][] indexs = new int[n][2];

        for (int i = 0; i < n; i++) {
            indexs[i][0] = arr[i];
            indexs[i][1] = i;
        }

        Arrays.sort(indexs, (o1, o2) -> o1[0] - o2[0]);

        int order = 0;
        int prevNum = indexs[0][0] - 1;
        for (int i = 0; i < n; i++) {
            if (prevNum == indexs[i][0]) {
                arr[indexs[i][1]] = order;
            } else {
                arr[indexs[i][1]] = ++order;
                prevNum = indexs[i][0];
            }
        }

        return arr;
    }
}
